package we.libQ
{
﻿  import flash.display.Bitmap;
﻿  import flash.display.BitmapData;
﻿  import flash.geom.Matrix;
﻿  import flash.geom.Point;
import flash.geom.Rectangle;
﻿  /**
﻿   * This class is designed to encapsulate all the functions you need to blit to a BitmapData. It is a singleton class. If you try to create a second instance, it will throw an error. If you lost the instance, call BlitRender.getInstance() to get it
﻿   * @author WorldEdit
﻿   */
﻿  public class BlitRender
﻿  {
﻿  ﻿  private static var instance:BlitRender;

﻿  ﻿  private var target:BitmapData;
﻿  ﻿  private var drawPoint:Point;
﻿  ﻿  private var drawMatrix:Matrix;

﻿  ﻿  /**
﻿  ﻿   * Creates a BlitRender instance. Since it's a meant singleton, but allows multiple instances for flexablility.
﻿  ﻿   */
﻿  ﻿  public function BlitRender()
﻿  ﻿  {
﻿  ﻿  ﻿  ﻿  drawPoint = new Point();
﻿  ﻿  ﻿  ﻿  drawMatrix = new Matrix();
﻿  ﻿  }

﻿  ﻿  /**
﻿  ﻿   * Sets the BitmapData target of the blitter.
﻿  ﻿   * @param targ  The BMD that is going to be blitted to
﻿  ﻿   */
﻿  ﻿  public function setTarget(targ:BitmapData):void
﻿  ﻿  {
﻿  ﻿  ﻿  target = targ;
﻿  ﻿  }

﻿  ﻿  /**
﻿  ﻿   * A quick function to blit a whole BitmapData to the target, no rect provided
﻿  ﻿   * @param source the BMD to blit from
﻿  ﻿   * @param x X coordinate for the upper left corner of the blitted image in destination bitmap
﻿  ﻿   * @param y Y coordinate for the upper left corner of the blitted image in destination bitmap
﻿  ﻿   */
﻿  ﻿  public function quickBlit(source:BitmapData,x:int=0,y:int=0,ox:int=0,oy:int=0):void
﻿  ﻿  {
﻿  ﻿  ﻿  if (! target)
﻿  ﻿  ﻿  {
﻿  ﻿  ﻿  ﻿  trace("2:null target");
﻿  ﻿  ﻿  ﻿  return;
﻿  ﻿  ﻿  }

﻿  ﻿  ﻿  drawPoint.x = x;
﻿  ﻿  ﻿  drawPoint.y = y;

﻿  ﻿  ﻿  target.copyPixels(source,source.rect,drawPoint,null,null,true);
﻿  ﻿  }

﻿  ﻿  /**
﻿  ﻿   * Allows quick blitting to any BMD, without setting the target
﻿  ﻿   * @param targ The BMD to blit to
﻿  ﻿   * @param source The BMD to blit from
﻿  ﻿   * @param x x Coordinate for the upper left corner of the blitted image in destination bitmap
﻿  ﻿   * @param y y Coordinate for the upper left corner of the blitted image in destination bitmap
﻿  ﻿   */
﻿  ﻿  public function quickBlitTo(targ:BitmapData,source:BitmapData,x:int=0,y:int=0):void
﻿  ﻿  {
﻿  ﻿  ﻿  drawPoint.x = x;
﻿  ﻿  ﻿  drawPoint.y = y;

﻿  ﻿  ﻿  targ.copyPixels(source,source.rect,drawPoint,null,null,true);
﻿  ﻿  }

﻿  ﻿  /**
﻿  ﻿   * A fully featured blit method. Not as quick as quickBlit, but more customizable. Blits to the current target.
﻿  ﻿   * @param source The BMD to blit from
﻿  ﻿   * @param x The x offset of the blitted pixels
﻿  ﻿   * @param y The y offset of the blitted pixels
﻿  ﻿   * @param rotation Indicates how much the image should be rotated
﻿  ﻿   * @param ox The x coordinate to rotate around (unnecessary if no rotation)
﻿  ﻿   * @param oy The y coordinate to rotate around (unnecessary if no rotation)
﻿  ﻿   * @param scaleX Indicates whether the image should be horizontally scaled
﻿  ﻿   * @param scaleY Indicates whether the image should be vertically scaled
﻿  ﻿   * @param blend The BlendMode to use for blitting
﻿  ﻿   */
﻿  ﻿  public function blit(source:BitmapData,x:int=0,y:int=0,rotation:int=0,ox:int=0,oy:int=0,scaleX:int=1,scaleY:int=1,blend:String='normal'):void
﻿  ﻿  {
﻿  ﻿  ﻿  //identity matrix
﻿  ﻿  ﻿  drawMatrix.a = drawMatrix.d = 1;
﻿  ﻿  ﻿  drawMatrix.c = drawMatrix.b =0 ;
	  drawMatrix.tx = -ox
	  drawMatrix.ty = -oy;
		
﻿  ﻿  ﻿  //transformations
﻿  ﻿  ﻿  if (scaleX != 1 || scaleY != 1)
﻿  ﻿  ﻿  {
﻿  ﻿  ﻿  ﻿  drawMatrix.a = scaleX;
﻿  ﻿  ﻿  ﻿  drawMatrix.d = scaleY;
		drawMatrix.tx *= scaleX;
		drawMatrix.ty *= scaleY;
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  if (rotation)
﻿  ﻿  ﻿  {
﻿  ﻿  ﻿  ﻿  var rad:Number = rotation * 0.017453293;
﻿  ﻿  ﻿  ﻿  var cosrad:Number = Math.cos(rad);
		var sinrad:Number = Math.sin(rad);
		drawMatrix.b = sinrad * drawMatrix.a;
﻿  ﻿  ﻿  ﻿  drawMatrix.a *= cosrad;
		drawMatrix.c = - sinrad * drawMatrix.d;
﻿  ﻿  ﻿  ﻿  drawMatrix.d *= cosrad;
		drawMatrix.ty = (drawMatrix.a + drawMatrix.b)*-oy;
		drawMatrix.tx = (drawMatrix.c + drawMatrix.d)*-ox;
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  if (x || y)
﻿  ﻿  ﻿  { 
		drawMatrix.tx += x+ox;
﻿  ﻿  ﻿  ﻿  drawMatrix.ty += y+oy;
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  //draw to the bitmap
﻿  ﻿  ﻿  target.draw(source,drawMatrix,null,blend);
﻿  ﻿  }

﻿  ﻿  /**
﻿  ﻿   * A fully featured blit method. Not as quick as quickBlit, but more customizable. Blits to any BMD without changing the target.
﻿  ﻿   * @param   targ The BMD to blit to
﻿  ﻿   * @param source The BMD to blit from
﻿  ﻿   * @param x The x offset of the blitted pixels
﻿  ﻿   * @param y The y offset of the blitted pixels
﻿  ﻿   * @param rotation Indicates how much the image should be rotated
﻿  ﻿   * @param ox The x coordinate to rotate around (unnecessary if no rotation)
﻿  ﻿   * @param oy The y coordinate to rotate around (unnecessary if no rotation)
﻿  ﻿   * @param scaleX Indicates whether the image should be horizontally scaled
﻿  ﻿   * @param scaleY Indicates whether the image should be vertically scaled
﻿  ﻿   * @param blend The BlendMode to use for blitting
﻿  ﻿   */
﻿  ﻿  public function blitTo(targ:BitmapData,source:BitmapData,x:int=0,y:int=0,ox:int=0,oy:int=0,rotation:int=0,scaleX:int=1,scaleY:int=1,blend:String='normal'):void
﻿  ﻿  {
﻿  ﻿  ﻿  //identity matrix
﻿  ﻿  ﻿  drawMatrix.a = drawMatrix.d = 1;
﻿  ﻿  ﻿  drawMatrix.c = drawMatrix.b = 0;
	  drawMatrix.tx = -ox;
	  drawMatrix.ty = -oy;
		
﻿  ﻿  ﻿  //transformations
﻿  ﻿  ﻿  if (scaleX != 1 || scaleY != 1)
﻿  ﻿  ﻿  {
﻿  ﻿  ﻿  ﻿  drawMatrix.a = scaleX;
﻿  ﻿  ﻿  ﻿  drawMatrix.d = scaleY;
		drawMatrix.tx *= scaleX;
		drawMatrix.ty *= scaleY;
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  if (rotation)
﻿  ﻿  ﻿  {
﻿  ﻿  ﻿  ﻿  var rad:Number = rotation * 0.017453293;
﻿  ﻿  ﻿  ﻿  var cosrad:Number = Math.cos(rad);
		var sinrad:Number = Math.sin(rad);
		drawMatrix.b = sinrad * drawMatrix.a;
﻿  ﻿  ﻿  ﻿  drawMatrix.a *= cosrad;
		drawMatrix.c = - sinrad * drawMatrix.d;
﻿  ﻿  ﻿  ﻿  drawMatrix.d *= cosrad;
		drawMatrix.ty = (drawMatrix.a + drawMatrix.b)*-oy;
		drawMatrix.tx = (drawMatrix.c + drawMatrix.d)*-ox;
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  if (x || y)
﻿  ﻿  ﻿  { 
		drawMatrix.tx += x+ox;
﻿  ﻿  ﻿  ﻿  drawMatrix.ty += y+oy;
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  //draw to the bitmap
﻿  ﻿  ﻿  targ.draw(source,drawMatrix,null,blend);
﻿  ﻿  }

﻿  ﻿  /**
﻿  ﻿   * Blits a View object to the target BMD.
﻿  ﻿   * @param view The View to be blitted from.
﻿  ﻿   */
﻿ /* ﻿  public function blitView(view:View):void
﻿  ﻿  {
﻿  ﻿  ﻿  if (view.rotation == 0 && view.scaleX == 1 && view.scaleY == 1)
﻿  ﻿  ﻿  {
﻿  ﻿  ﻿  ﻿  quickBlit(view.graphic,int(view.x),int(view.y),int(view.originX),int(view.originY));
﻿  ﻿  ﻿  }
﻿  ﻿  ﻿  else
﻿  ﻿  ﻿  {
﻿  ﻿  ﻿  ﻿  blit(view.graphic,int(view.x),int(view.y),int(view.originX),int(view.originY),view.rotation,view.scaleX,view.scaleY);
﻿  ﻿  ﻿  }
﻿  ﻿  }*/
﻿  }
}